package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.AdminRole;
import com.atguigu.entity.Role;
import com.atguigu.mapper.AdminRoleMapper;
import com.atguigu.mapper.RoleMapper;
import com.atguigu.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 翟文海
 * @Date: 2022/11/2/002 15:27
 * @Version: 1.0
 * @Description:
 **/
@Service(interfaceClass = AdminRoleService.class)
public class AdminRoleServiceImpl extends BaseServiceImpl<AdminRole> implements AdminRoleService {
    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private RoleMapper roleMapper;


    @Override
    protected BaseMapper<AdminRole> getMapper() {
        return adminRoleMapper;
    }

    @Override
    public Map<String, List<Role>> findRoleListMapByAdminId(Long id) {
        //查找所有角色
        List<Role> roleList = roleMapper.findAll();
        //查找admin的角色
        List<Long> assignRoleIdList = adminRoleMapper.findRoleIdListByAdminId(id);
        //用户没有分配角色时会为空
        Optional<List<Long>> optionalLongs = Optional.ofNullable(assignRoleIdList);
        //为空的话就选一个备胎供后面使用
        optionalLongs.orElse(new ArrayList<Long>());
        List<Role> assignRoleList = roleList.stream()
                .filter(role -> optionalLongs.get().contains(role.getId())).collect(Collectors.toList());
        List<Role> unAssignRoleList = roleList.stream()
                .filter(role -> !optionalLongs.get().contains(role.getId())).collect(Collectors.toList());
        Map<String, List<Role>> map = new HashMap<>();
        map.put("unAssignRoleList", unAssignRoleList);
        map.put("assignRoleList", assignRoleList);
        return map;
    }

    /**
     * 进行角色分配
     * @param adminId 用户id
     * @param roleIds 需要分配的新角色
     * @return
     *//*
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void assignRole(Long adminId, List<Long> roleIds) {
        //1.将此用户所有关联的角色都删掉
        adminRoleMapper.delete(adminId);
        //2.将此用户需要关联的角色重新关联起来，插入数据
        Optional<List<Long>> longList = Optional.ofNullable(roleIds);
        longList.orElse(new ArrayList<>());
        List<AdminRole> adminRoles= new ArrayList<>();

        roleIds.stream().forEach(roleId->{
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRoles.add(adminRole);
        });
        if (!CollectionUtils.isEmpty(adminRoles)) {
            adminRoleMapper.insertBatch(adminRoles);
        }
    }*/

    @Transactional
    @Override
    public void assignRole(Long adminId, List<Long> roleIds) {
        //1.右边现在要分配给用户的角色和此用户之前的角色查询出来，将现在没有的角色进行删除
        // 空处理
        Optional<List<Long>> longList = Optional.ofNullable(roleIds);
        longList.orElse(new ArrayList<>());

        //1.1此用户之前的拥有的角色
        List<Long> originalRoleIds = adminRoleMapper.findListByAdminId(adminId);
        //需要删除的角色
        List<Long> removeList = originalRoleIds.stream()
                .filter(originalRoleId -> !longList.get().contains(originalRoleId))
                .collect(Collectors.toList());
        if (longList.get().size() == 0) {
            removeList = originalRoleIds;
        }
        if (removeList.size()!=0) {
            adminRoleMapper.deleteBatch(adminId,removeList);
        }
        //2.右边现在要分配给用户的角色进行保存，将之前关注过的或没有关注的进行保存
        longList.get().forEach(roleId->{
            //2.1查询出用户拥有过的角色
            AdminRole adminRole = adminRoleMapper.findByAdminIdAndRoleId(adminId, roleId);
            if (adminRole!=null){
                //之前关联过的角色，但是被删除了
                if (adminRole.getIsDeleted() == 1) {
                    adminRole.setIsDeleted(0);
                    //将数据进行更新
                    adminRoleMapper.update(adminRole);
                }
            }else {
                AdminRole newAdminRole = new AdminRole();
                newAdminRole.setRoleId(roleId);
                newAdminRole.setAdminId(adminId);
                adminRoleMapper.insert(newAdminRole);
            }
        });
    }

}
